#ifndef SDLWINDOW_H
#define SDLWINDOW_H

#include "common.h"

const int W = 800;
const int H = 600;

int sdl_print_bpp()
{
	const SDL_VideoInfo* vi;
	const SDL_PixelFormat* pf;
	vi = SDL_GetVideoInfo();
	if(!vi)
		sdl_error();
	pf = vi->vfmt;
	printf("we need bpp 32, check what we have\n");
	printf("bpp %d\n", pf->BitsPerPixel);
	printf("we need timer step 10 milisecond\n");
	printf("fps must be 62 - 63\n");
	return 1;
}

int sdl_preset_opengl()
{
	if(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8))
		sdl_error();
    if(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8))
    	sdl_error();
    if(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8))
    	sdl_error();
    if(SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16))
    	sdl_error();
    if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1))
    	sdl_error();
    return 1;
}

int sdl_create_window()
{
	if(SDL_Init(SDL_INIT_VIDEO))
		sdl_error();
	sdl_print_bpp();
	sdl_preset_opengl();
	SDL_WM_SetCaption("Rects Fight", 0);
    if (!SDL_SetVideoMode(W, H, 32, SDL_OPENGL 
		| SDL_NOFRAME))
    	sdl_error();
    
	return 1;
}

const int Wait_Clear = 1;
const int Wait_Normal = 2;

int wait(int mode) 
{
    // 16 milisecs per frame
    const float stable_frame = 1000.0f / 60.0f; 
    const int hard_step = 10;
    static float start_time = 0;
    static float speed_up = 0;
    float frame_time;
    float delay, free_time, end_time;
    int sleep_time;
    
    if(mode == Wait_Clear)
    {
        start_time = SDL_GetTicks();
        speed_up = 0;
        return 0;
    }
    
    if(start_time == 0)
        start_time = SDL_GetTicks();
    end_time = SDL_GetTicks();    
    frame_time = end_time - start_time;
    start_time = end_time;
    if(frame_time == stable_frame)
        return 1;
    // if frame time is slower saving speedup value
    if(frame_time > stable_frame) {
        speed_up += frame_time - stable_frame;
        return 1; 
    }
    // if frame time is quicker we wait by 10 milisecs
    if(frame_time < stable_frame) {
        free_time = stable_frame - frame_time;
        // lowering the tail
        speed_up -= free_time;
        // if we just shorted the gap
        if(speed_up >= 0)
            return 1;
        // if we need to slow
        delay = -speed_up;
        if(delay < hard_step)
            // we can not sleep such short time
            return 1;
        sleep_time = ((int)delay / (int)hard_step) *
            (int)hard_step;
        //sleep_time = hard_step;
        SDL_Delay(sleep_time);
        speed_up += sleep_time;
        start_time += sleep_time;
    }
    return 1;
}

int print_fps()
{
    static Uint32 last_time = 0;
    static Uint32 frame_count = 0;
    static Uint32 frame_time = 0;
    Uint32 time, delta_time;
    int fps;
    char buffer[64];
    
    if(last_time == 0)
        last_time = SDL_GetTicks();
    time = SDL_GetTicks();
    delta_time = time - last_time;
    last_time = time;
    frame_count++;
    frame_time+=delta_time;
    if(frame_time >= 1000) {
        fps = frame_count;
        if((fps > 55) && (fps < 65)) {
            sprintf(buffer, "Rects Fight");
        } else {
            sprintf(buffer, "Rects Fight fps %d\n", fps);
        }
        printf("%d ", fps);
        SDL_WM_SetCaption(buffer, 0);
        frame_count = 0;
        frame_time -= 1000;
    }
    return 1;
}

#endif
